package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.SysRoleMapper;
import com.atguigu.spzx.manager.mapper.SysRoleMenuMapper;
import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.exception.GuiguException;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author LT
 * @create 2024-03-11-19:04
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public PageInfo<SysRole> findByKeyword(Integer pageNum, Integer pageSize, String keyword) {

        PageHelper.startPage(pageNum, pageSize);
        List<SysRole> list = sysRoleMapper.findByKeyword(keyword);
        PageInfo<SysRole> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void addSysRole(SysRole sysRole) {
        sysRoleMapper.addSysRole(sysRole);
    }

    @Override
    public SysRole findById(Integer rid) {
        SysRole sysRole = sysRoleMapper.findById(rid);
        return sysRole;
    }

    @Override
    public void updateById(SysRole sysRole) {
        sysRoleMapper.updateById(sysRole);
    }

    @Override
    public void deleteById(Integer rid) {
        sysRoleMapper.deleteById(rid);
    }

    @Override
    public List<SysRole> findAll() {
        List<SysRole> sysRoles = sysRoleMapper.findAll();
        return sysRoles;
    }

    @Override
    public List<SysRole> findAllRoles(Integer userId) {
        List<SysRole> sysRoles = sysRoleMapper.findAllRoles(userId);
        return sysRoles;
    }

    @Override
    public void assignMenus2Role(Long roleId, List<Long> menuIds) {
        sysRoleMenuMapper.deletedByRoleId(roleId);
        if (CollectionUtils.isEmpty(menuIds)) {
            return;
        }
        sysRoleMenuMapper.assignMenus2Role(roleId, menuIds);
    }
}
